#pragma once

//-------------------------------------------------------------------------------------
class CDownloadMonitor : public FCTimerNotify
{
    int    m_task_id ;
    DWORD  m_start_time ;
    HWND   m_dlg ;
public:
    CDownloadMonitor (HWND hDlg, int nTaskID)
    {
        m_dlg = hDlg ;
        m_task_id = nTaskID ;
        m_start_time = GetTickCount() ;
        StartTimer(500) ;
    }

    // in milliseconds
    int GetElapseTime() const
    {
        return (int)(GetTickCount() - m_start_time) ;
    }

private:
    // Update progress
    virtual void OnHandleTimer()
    {
        PostMessage (m_dlg, UIS_WM_UPDATE_PROGRESS_UI, (WPARAM)m_task_id, 0) ;
    }
};

//-------------------------------------------------------------------------------------
class DlgSimpleDownload : public CDialog,
                          public FCHttpDownloadManager
{
    enum
    {
        IDC_EDIT_URL = 1200,
        IDC_EDIT_START = 1201,
        IDC_EDIT_AGENT = 1202,
        IDC_EDIT_FIELD = 1203,

        IDC_TEXT_STATUS = 1300,
        IDC_TEXT_TOTAL = 1301,
        IDC_TEXT_DOWNLOAD = 1302,
        IDC_TEXT_CURRENT_SPEED = 1303,
        IDC_TEXT_AVERAGE_SPEED = 1304,
        IDC_TEXT_ELAPSE_TIME = 1305,
        IDC_TEXT_MD5 = 1306,

        IDC_SAVE_TO_FILE = 1500,
        IDC_SET_PROXY = 1501,
        IDC_DOWN_PROGRESS = 1600,

        IDC_TEXT_SEND_HEADER = 1800,
        IDC_TEXT_RESPONSE_HEADER = 1801,
    };

public:
    DlgSimpleDownload(CWnd* pParent = NULL) : CDialog(IDD_SIMPLE_DOWNLOAD, pParent)
    {
        m_tmp_data_file = FCFileEx::QueryTempFilePath() ;
    }
    ~DlgSimpleDownload()
    {
        DeleteFile(m_tmp_data_file) ;
    }

private:
    std::auto_ptr<CDownloadMonitor>   m_monitor ;

    void ClearOutputText() ;

    CString   m_tmp_data_file ;
    void AcceptReceivedData (FCHttpDownload* pTask) ;

    void FinishUpdateStatus (FCHttpDownload* pTask) ;

    virtual void OnAfterDownloadConnected (FCHttpDownload* pTask) ;
    virtual void OnAfterDownloadFinish (FCHttpDownload* pTask) ;

    virtual BOOL OnInitDialog();
    virtual void OnOK();
    afx_msg void OnSaveToFile();
    afx_msg void OnSetProxy();
    afx_msg LRESULT OnUpdateProgressUI(WPARAM wParam, LPARAM lParam) ;
	DECLARE_MESSAGE_MAP()
};
